
# -*- coding: utf-8 -*-
from django.db import models
from datetime import datetime
from django.utils.safestring import mark_safe

# Create your models here.
class MessageTemplet(models.Model):

    id = models.AutoField(verbose_name='id', primary_key=True)
    template_name = models.CharField(max_length=150, verbose_name='模板名称', default='', blank=False)
    sign_name = models.CharField(max_length=150, verbose_name='短信签名',default='',blank=False)
    template = models.TextField(max_length=250,verbose_name='模板内容',default='',blank=False)
    template_code = models.CharField(max_length=250,verbose_name='模板code',default='',blank=True,help_text='审核通过后由技术人员添加')
    template_illustrate = models.TextField(verbose_name='模板申请说明',default='',help_text='请说明模板用途')
    template_type = models.CharField(verbose_name='短信类型', default='1', max_length=20,
                                       choices=(
                                           ('1', '验证码'),
                                           ('2', '短信通知'),
                                           ('3', '推广短信'),
                                       ),
                                  help_text='验证码短信：用于发送验证码类短信，如登录验证、支付确认、登录异常等；短信通知：用于发送系统通知类短信，如物流通知、付款回执、状态通知等；推广短信：用于发送营销推广类短信，如会员关怀、新品上线、活动通知等'
                                  )
    create_time = models.DateTimeField(default=datetime.now, blank=True, verbose_name='添加时间')
    status = models.IntegerField(verbose_name='状态', default=0, null=True)
    desc = models.CharField(max_length=150, verbose_name='审核描述',default='',blank=False)

    class Meta:
        verbose_name = '短信模板管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.id)




    def change_status(self):
        if self.status == 0 :
            return mark_safe("<p style='color:grey';>审核中</p>")
        elif self.status == 1 :
            return mark_safe("<p style='color:red';>不通过</p>")
        elif self.status == 2:
            return mark_safe("<p style='color:green';>通过</p>")

    change_status.short_description = "状态"

    #状态1显示两个按钮，状态2“不通过”显示“通过”两个按钮，状态3“通过”显示不通过按钮
    def change_button(self):
        if self.status == 0:
            return mark_safe('<input id="used" type="button" data-toggle="modal" data-target="#playModal" class="btn btn-primary" onclick="startUse('+str(self.id)+')" value="通&nbsp;&nbsp;&nbsp;&nbsp;过">&nbsp;&nbsp;<input id="not-used" type="button" data-toggle="modal" data-target="#pauseModal" class="btn btn-danger" onclick="stopUse('+str(self.id)+')" value="不&nbsp;通&nbsp;过">')
        elif self.status == 1:
            return mark_safe('<input id="used" type="button" data-toggle="modal" data-target="#playModal" class="btn btn-primary" onclick="startUse('+str(self.id)+')" value="通&nbsp;&nbsp;&nbsp;&nbsp;过">')
        elif self.status == 2:
            return mark_safe('<input id="not-used" type="button" data-toggle="modal" data-target="#pauseModal" class="btn btn-danger" onclick="stopUse(' + str(self.id) + ')" value="不&nbsp;通&nbsp;过">')

    change_button.short_description = "操作"

class MessageTempletNoButton(MessageTemplet):
    class Meta:
        verbose_name = "短信模板"
        verbose_name_plural = verbose_name
        proxy = True


